package com.mark.controller.portal;

import com.github.pagehelper.PageInfo;
import com.mark.common.ServerResponse;
import com.mark.controller.common.CurrentUser;
import com.mark.enums.UserStateEnum;
import com.mark.pojo.Product;
import com.mark.pojo.User;
import com.mark.service.IProductService;
import com.mark.vo.ProductDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created by Mark on 2018/3/27.
 * 商品操作前端控制
 */

@Controller
@RequestMapping("/product/")
public class ProductController {
    @Autowired
    private IProductService iProductService;

    //1.根据商品Id查找商品详情
    @RequestMapping(value="product_detail.do" , method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<ProductDetailVo> productDetail(Integer productId){
        return iProductService.portalGetProductDetail(productId);
    }

    //2.根据商品名，商品Id分页组合查询
    @RequestMapping(value="product_list.do" , method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "keyword",required = false)String keyword,
                                         @RequestParam(value = "categoryId",required = false)Integer categoryId,
                                         @RequestParam(value = "ownerId",required = false)Integer ownerId,
                                         @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                         @RequestParam(value = "orderBy",defaultValue = "") String orderBy){
        return iProductService.getProductByKeywordCategory(keyword,categoryId,ownerId,pageNum,pageSize,orderBy);
    }

    //--------------------------- seller operation------------------------------------------------------------
    //1.保存商品（添加，更新）
    @RequestMapping(value="save_product.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse productSave(HttpSession session, Product product, HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() != UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createBySellerNeedLogin();
        }
        product.setOwnerId(currentUser.getId());
        return iProductService.insertOrUpdateProduct(product);
    }

    //2.修改产品销售状态
    @RequestMapping(value="set_sale_status.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setSaleStatus(Integer productId,Integer status,HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() != UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createBySellerNeedLogin();
        }
        return  iProductService.setSaleStatus(productId,status);
    }

    //3.获取商品详情
    @RequestMapping(value="seller_product_detail.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse productSellerDetail(Integer productId,HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() != UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createBySellerNeedLogin();
        }
        return iProductService.getProductDetail(productId);
    }

    //4.商品按创建时间升序分页显示
    @RequestMapping(value="seller_product_list.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getList(Integer ownerId,
                                  @RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                  @RequestParam(value = "pageSize",defaultValue = "10") int pageSize,
                                  HttpServletRequest httpServletRequest) {
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() != UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createBySellerNeedLogin();
        }
        return iProductService.getProductList(pageNum,pageSize,ownerId);
    }

    //5.商品搜索：（1：按商品名模糊搜索）（2：按Id搜索）（3：按状态搜索）
    @RequestMapping(value="search_product.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse searchProduct(String productName,
                                        Integer productId,
                                        Integer status,
                                        Integer ownerId,
                                        @RequestParam(value = "pageNum",defaultValue ="1" ) int pageNum,
                                        @RequestParam(value = "pageSize",defaultValue ="10" )int pageSize,
                                        HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() != UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createBySellerNeedLogin();
        }
        return iProductService.searchProduct(ownerId,productName,productId,status,pageNum,pageSize);
    }

    //6.卖家数据统计
    @RequestMapping(value="seller_pro_data.do" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse getSellerProData(HttpServletRequest httpServletRequest){
        ServerResponse response = CurrentUser.getCurrentUser(httpServletRequest);
        if (!response.isSuccess()){
            return response;
        }
        User currentUser = (User) response.getData();
        if(currentUser.getRole() != UserStateEnum.SELLER_CUSTOMER.getRole()){
            return ServerResponse.createBySellerNeedLogin();
        }
        return iProductService.getSellerProData(currentUser.getId());
    }
}
